﻿package view.popup
{
    import cmd.*;
    import com.core.*;
    import com.core.display.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.net.*;
    import flash.text.*;
    import manager.*;
    import morn.core.components.*;
    import plugin.store.*;
    import view.message.*;
    import view.uilt.*;

    public class NewSureBuyStoreUI extends View implements IDialogUI
    {
        public var closeBtn:Button;
        public var sureBtn:Button;
        public var cancelBtn:Button;
        public var leftBtn:Button;
        public var rightBtn:Button;
        public var yuanbaoImg:Image;
        public var jifen:Image;
        public var maxBtn:Button;
        public var cashImg:Image;
        public var label_itemname:Label;
        public var box_yh:Box;
        public var label_money:Label;
        public var label_money1:Label;
        public var lab_xiangou:Label;
        protected var uiXML:XML;
        private var amounttext:TextField;
        private var allpricetext:TextField;
        private var item:Loader;
        private var curCount:int = 1;
        private var data:Object;
        private var lcount:int = 0;
        private var background:Sprite;
        private var Max:int = 0;
        private var priceValue:int = 0;

        public function NewSureBuyStoreUI()
        {
            this.uiXML = <View>r
n	t	t	t  <Image url=""png.common.cs2_tsbg"" x=""0"" y=""0"" width=""357"" height=""295"" sizeGrid=""10,50,10,50""/>r
n	t	t	t  <Button skin=""png.common.btn_cs2_close"" x=""326"" y=""5"" var=""closeBtn""/>r
n	t	t	t  <Button skin=""png.common.btn_cs2_bag"" x=""58"" y=""230"" var=""sureBtn"" label=""确定"" labelColors=""0xb29a6c,0xb29a6c,0xb29a6c"" labelFont=""SimSun"" labelStroke=""1""/>r
n	t	t	t  <Button skin=""png.common.btn_cs2_bag"" x=""216"" y=""230"" var=""cancelBtn"" label=""取消"" labelColors=""0xb29a6c,0xb29a6c,0xb29a6c"" labelStroke=""1"" labelFont=""SimSun""/>r
n	t	t	t  <Button skin=""png.common.btn_zuo"" x=""137"" y=""164"" var=""leftBtn""/>r
n	t	t	t  <Button skin=""png.common.btn_jiantou"" x=""242"" y=""164"" var=""rightBtn""/>r
n	t	t	t  <Image url=""png.common.s9_itembg"" x=""155"" y=""160"" width=""78"" sizeGrid=""5,16,5,29""/>r
n	t	t	t  <Image url=""png.common.img_2"" x=""57"" y=""76""/>r
n	t	t	t  <Image url=""png.common.s9_itembg"" x=""129"" y=""191"" width=""180"" smoothing=""false"" sizeGrid=""10,10,10,10""/>r
n	t	t	t  <Image url=""png.common.cs2_yuanbao"" x=""135"" y=""199"" var=""yuanbaoImg""/>r
n	t	t	t  <Image url=""png.common.jifen"" x=""137"" y=""197"" visible=""false"" var=""jifen"" width=""20"" height=""16""/>r
n	t	t	t  <Button label=""最大"" skin=""png.common.btn_max"" x=""262"" y=""164"" labelFont=""SimSun"" labelStroke=""1"" labelColors=""0xb29a6c,0xb29a6c,0xb29a6c"" var=""maxBtn""/>r
n	t	t	t  <Label text=""确认"" x=""165"" y=""4"" color=""0xffe4c7"" bold=""true"" font=""Microsoft YaHei"" size=""15""/>r
n	t	t	t  <Label text=""购买数量"" x=""72"" y=""167"" stroke=""1"" font=""SimSun"" color=""0xb8764a""/>r
n	t	t	t  <Label text=""所需总额"" x=""72"" y=""197"" stroke=""1"" font=""SimSun"" color=""0xb8764a""/>r
n	t	t	t  <Image url=""png.common.cs2_qian"" x=""135"" y=""199"" var=""cashImg""/>r
n	t	t	t  <Label text=""神仙药水"" x=""148"" y=""64"" stroke=""1"" font=""SimSun"" color=""0xb8764a"" width=""149"" height=""18"" var=""label_itemname"" bold=""true""/>r
n	t	t	t  <Box x=""144"" y=""86"" var=""box_yh"">r
n	t	t	t	t<Label text=""原价："" y=""4"" stroke=""1"" font=""SimSun"" color=""0x53b436""/>r
n	t	t	t	t<Label text=""售价："" x=""1"" y=""28"" stroke=""1"" font=""SimSun"" color=""0x53b436""/>r
n	t	t	t	t<Image url=""png.common.cs2_selectbg2"" x=""35"" y=""2"" height=""50""/>r
n	t	t	t	t<Image url=""png.common.cs2_yuanbao"" x=""35"" y=""29"" width=""22"" height=""16""/>r
n	t	t	t	t<Image url=""png.common.cs2_yuanbao"" x=""35"" y=""4""/>r
n	t	t	t	t<Label text=""1000"" x=""62"" y=""6"" stroke=""1"" font=""SimSun"" color=""0xb8764a"" width=""76"" height=""18"" var=""label_money""/>r
n	t	t	t	t<Label text=""1000"" x=""63"" y=""29"" stroke=""1"" font=""SimSun"" color=""0xb8764a"" width=""77"" height=""18"" var=""label_money1""/>r
n	t	t	t	t<Label text=""限购10"" x=""139"" y=""27"" stroke=""1"" font=""SimSun"" color=""0x53b436"" width=""40"" height=""16"" var=""lab_xiangou""/>r
n	t	t	t	t<Label stroke=""1"" font=""SimSun"" color=""0xff0000"" underline=""true"" text=""               "" width=""94"" height=""16""/>r
n	t	t	t  </Box>r
n	t	t	t</View>")("<View>
			  <Image url="png.common.cs2_tsbg" x="0" y="0" width="357" height="295" sizeGrid="10,50,10,50"/>
			  <Button skin="png.common.btn_cs2_close" x="326" y="5" var="closeBtn"/>
			  <Button skin="png.common.btn_cs2_bag" x="58" y="230" var="sureBtn" label="确定" labelColors="0xb29a6c,0xb29a6c,0xb29a6c" labelFont="SimSun" labelStroke="1"/>
			  <Button skin="png.common.btn_cs2_bag" x="216" y="230" var="cancelBtn" label="取消" labelColors="0xb29a6c,0xb29a6c,0xb29a6c" labelStroke="1" labelFont="SimSun"/>
			  <Button skin="png.common.btn_zuo" x="137" y="164" var="leftBtn"/>
			  <Button skin="png.common.btn_jiantou" x="242" y="164" var="rightBtn"/>
			  <Image url="png.common.s9_itembg" x="155" y="160" width="78" sizeGrid="5,16,5,29"/>
			  <Image url="png.common.img_2" x="57" y="76"/>
			  <Image url="png.common.s9_itembg" x="129" y="191" width="180" smoothing="false" sizeGrid="10,10,10,10"/>
			  <Image url="png.common.cs2_yuanbao" x="135" y="199" var="yuanbaoImg"/>
			  <Image url="png.common.jifen" x="137" y="197" visible="false" var="jifen" width="20" height="16"/>
			  <Button label="最大\" skin="png.common.btn_max" x="262" y="164" labelFont="SimSun" labelStroke="1" labelColors="0xb29a6c,0xb29a6c,0xb29a6c" var="maxBtn"/>
			  <Label text="确认" x="165" y="4" color="0xffe4c7" bold="true" font="Microsoft YaHei" size="15"/>
			  <Label text="购买数量" x="72" y="167" stroke="1" font="SimSun" color="0xb8764a"/>
			  <Label text="所需总额" x="72" y="197" stroke="1" font="SimSun" color="0xb8764a"/>
			  <Image url="png.common.cs2_qian" x="135" y="199" var="cashImg"/>
			  <Label text="神仙药水" x="148" y="64" stroke="1" font="SimSun" color="0xb8764a" width="149" height="18" var="label_itemname" bold="true"/>
			  <Box x="144" y="86" var="box_yh">
				<Label text="原价：\" y="4" stroke="1" font="SimSun" color="0x53b436"/>
				<Label text="售价：\" x="1" y="28" stroke="1" font="SimSun" color="0x53b436"/>
				<Image url="png.common.cs2_selectbg2" x="35" y="2" height="50"/>
				<Image url="png.common.cs2_yuanbao" x="35" y="29" width="22" height="16"/>
				<Image url="png.common.cs2_yuanbao" x="35" y="4"/>
				<Label text="1000" x="62" y="6" stroke="1" font="SimSun" color="0xb8764a" width="76" height="18" var="label_money"/>
				<Label text="1000" x="63" y="29" stroke="1" font="SimSun" color="0xb8764a" width="77" height="18" var="label_money1"/>
				<Label text="限购10" x="139" y="27" stroke="1" font="SimSun" color="0x53b436" width="40" height="16" var="lab_xiangou"/>
				<Label stroke="1" font="SimSun" color="0xff0000" underline="true" text="               " width="94" height="16"/>
			  </Box>
			</View>;
            return;
        }// end function

        override protected function createChildren() : void
        {
            createView(this.uiXML);
            this.init();
            return;
        }// end function

        public function log(param1:String, param2:int = 0) : void
        {
            return;
        }// end function

        override protected function initialize() : void
        {
            super.initialize();
            return;
        }// end function

        public function init() : void
        {
            this.background = new Sprite();
            this.background.graphics.beginFill(0, 0.5);
            this.background.graphics.drawRect(0, 0, GlobalLayer.instance.root.stage.stageWidth, GlobalLayer.instance.root.stage.stageHeight);
            this.background.graphics.endFill();
            this.background.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            PluginManager.instance.alertContainer.addChild(this.background);
            var _loc_2:Boolean = false;
            this.visible = false;
            this.background.visible = _loc_2;
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            var _loc_1:* = new TextFormat("宋体", 12, null, null, null, null, null, null, TextFormatAlign.CENTER);
            this.amounttext = new TextField();
            this.amounttext.x = 155;
            this.amounttext.y = 165;
            this.amounttext.width = 72;
            this.amounttext.height = 20;
            this.amounttext.defaultTextFormat = _loc_1;
            this.amounttext.text = String(this.curCount);
            this.amounttext.textColor = MColor.V_NORMAL;
            this.amounttext.type = TextFieldType.INPUT;
            this.amounttext.maxChars = 10;
            this.amounttext.restrict = "0-9";
            this.amounttext.addEventListener(MouseEvent.CLICK, this.inputClickHandler);
            this.amounttext.addEventListener(Event.CHANGE, this.onInput);
            this.addChild(this.amounttext);
            this.allpricetext = new TextField();
            this.allpricetext.x = 132;
            this.allpricetext.y = 197;
            this.allpricetext.width = 170;
            this.allpricetext.height = 20;
            this.allpricetext.defaultTextFormat = _loc_1;
            this.allpricetext.textColor = MColor.V_NORMAL;
            this.allpricetext.autoSize = TextFieldAutoSize.CENTER;
            this.addChild(this.allpricetext);
            this.leftBtn.addEventListener(MouseEvent.CLICK, this.onLeft);
            this.rightBtn.addEventListener(MouseEvent.CLICK, this.onRight);
            this.sureBtn.label = "确认";
            this.sureBtn.labelFont = "宋体";
            this.sureBtn.labelColors = "0xb29a6c, 0xb29a6c, 0xb29a6c";
            this.sureBtn.addEventListener(MouseEvent.CLICK, this.onSure);
            this.cancelBtn.label = "取消";
            this.cancelBtn.labelFont = "宋体";
            this.cancelBtn.labelColors = "0xb29a6c, 0xb29a6c, 0xb29a6c";
            this.cancelBtn.addEventListener(MouseEvent.CLICK, this.onClose);
            this.maxBtn.addEventListener(MouseEvent.CLICK, this.onMax);
            this.closeBtn.addEventListener(MouseEvent.CLICK, this.onClose);
            this.addEventListener(MouseEvent.CLICK, this.onStageClicked);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            GlobalLayer.instance.root.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function inputClickHandler(event:MouseEvent) : void
        {
            GlobalLayer.instance.flashStage.focus = this.amounttext;
            event.stopImmediatePropagation();
            return;
        }// end function

        private function onInput(event:Event) : void
        {
            this.curCount = int(this.amounttext.text);
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onLeft(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.curCount - 1;
            _loc_2.curCount = _loc_3;
            if (this.curCount < 1)
            {
                this.curCount = 1;
            }
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onRight(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.curCount + 1;
            _loc_2.curCount = _loc_3;
            if (this.curCount >= 99)
            {
                this.curCount = 99;
            }
            if (this.lcount > 0 && this.curCount >= this.lcount)
            {
                this.curCount = this.lcount;
            }
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onMax(event:MouseEvent) : void
        {
            if (this.lcount == 0)
            {
                this.changeNumText(99);
            }
            else
            {
                this.changeNumText(this.lcount);
            }
            return;
        }// end function

        private function onStageClicked(event:MouseEvent) : void
        {
            GlobalLayer.instance.flashStage.focus = null;
            this.curCount = int(this.amounttext.text);
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function changeNumText(param1:int) : void
        {
            var _loc_2:* = ModelProxy.getValue("data", "bag.money") + ModelProxy.getValue("data", "bag.bonus");
            var _loc_3:* = ModelProxy.getValue("data", "bag.gold");
            if (param1 > 999)
            {
                param1 = 999;
            }
            var _loc_4:int = 0;
            if (this.data && this.data.type == 6)
            {
                _loc_4 = int(_loc_3 / this.priceValue);
            }
            else
            {
                _loc_4 = int(_loc_2 / this.priceValue);
            }
            if (_loc_4 > this.Max)
            {
                _loc_4 = this.Max;
            }
            if (param1 > _loc_4)
            {
                param1 = _loc_4;
            }
            this.amounttext.text = String(param1);
            this.curCount = param1;
            if (this.data == null)
            {
                this.allpricetext.text = "";
                return;
            }
            this.allpricetext.text = String(param1 * this.priceValue);
            return;
        }// end function

        private function onSure(event:MouseEvent) : void
        {
            var e:* = event;
            var count:* = int(this.amounttext.text);
            if (count == 0)
            {
                if (this.data.hasOwnProperty("shoptype") && this.data.shoptype == 1)
                {
                    return;
                }
                if (this.data.type == 8 || this.data.type == 9 || this.data.type == 10)
                {
                    Alert2.show("积分不足无法兑换", "", ["确认"]);
                }
                else
                {
                    Alert2.show("元宝不足前往充值", "", ["确认", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    if (!ItemUilt.canpay)
                    {
                        ModelProxy.change("movetips", "error", "功能尚未开放，敬请期待");
                        return;
                    }
                    ExternalInterface.call("topay");
                }
                return;
            }// end function
            );
                }
            }
            else if (this.data.hasOwnProperty("shoptype") && this.data.shoptype == 1)
            {
                PluginManager.instance.sendToServer("shop", CMDShop.MarkBuyItem2, {id:this.data.id, count:count});
            }
            else if (this.data.type == 8 || this.data.type == 9 || this.data.type == 10)
            {
                PluginManager.instance.sendToServer("shop", CMDShop.MarkBuyItem, {id:this.data.id, count:count});
            }
            else
            {
                PluginManager.instance.sendToServer("store", CmdStore._3BUY, {id:this.data.id, count:count, type:this.data.type, activetype:int(this.data.dz)});
            }
            this.close();
            return;
        }// end function

        public function show(param1:Object) : void
        {
            if (param1 == null)
            {
                return;
            }
            this.lcount = 0;
            this.data = param1;
            if (int(this.data.itemneed) > 0)
            {
                this.box_yh.visible = true;
                this.label_money.text = this.data.price;
                this.label_money1.text = this.data.nowprice;
                this.lcount = this.data.lcount;
                this.lab_xiangou.text = "限购" + this.lcount;
            }
            else
            {
                this.box_yh.visible = false;
            }
            this.darwItem();
            this.label_itemname.text = this.data.name1;
            var _loc_2:int = 0;
            if (this.data.type == 2)
            {
                this.cashImg.visible = true;
                this.yuanbaoImg.visible = false;
                this.jifen.visible = false;
                _loc_2 = ModelProxy.getValue("data", "bag.bonus");
            }
            else if (this.data.type == 8)
            {
                this.cashImg.visible = false;
                this.yuanbaoImg.visible = false;
                this.jifen.visible = true;
                _loc_2 = ModelProxy.getValue("data", "bag.recy");
            }
            else if (this.data.type == 3)
            {
                this.cashImg.visible = false;
                this.yuanbaoImg.visible = true;
                this.jifen.visible = false;
                _loc_2 = ModelProxy.getValue("data", "bag.money");
            }
            else if (this.data.type == 9)
            {
                this.cashImg.visible = false;
                this.yuanbaoImg.visible = false;
                this.jifen.visible = true;
                _loc_2 = ModelProxy.getValue("data", "bag.zuoqi");
            }
            else if (this.data.type == 10)
            {
                this.cashImg.visible = false;
                this.yuanbaoImg.visible = false;
                this.jifen.visible = true;
                _loc_2 = ModelProxy.getValue("data", "bag.yuyi");
            }
            else if (this.data.type == 6)
            {
                this.cashImg.visible = false;
                this.yuanbaoImg.visible = false;
                this.jifen.visible = false;
                _loc_2 = ModelProxy.getValue("data", "bag.gold");
            }
            else if (this.data.type == 14)
            {
                this.cashImg.visible = false;
                this.yuanbaoImg.visible = false;
                this.jifen.visible = true;
                _loc_2 = ModelProxy.getValue("data", "bag.honor");
            }
            if (int(this.data.dz) > 0 || this.lcount > 0)
            {
                this.priceValue = this.data.nowprice;
                this.allpricetext.text = this.data.nowprice;
            }
            else
            {
                this.priceValue = this.data.price;
                this.allpricetext.text = this.data.price;
            }
            var _loc_3:* = int(_loc_2 / this.priceValue);
            var _loc_4:int = 1;
            var _loc_5:int = 99;
            if (this.data.itemtype == 1)
            {
                _loc_3 = 1;
                _loc_5 = 1;
            }
            else if (_loc_3 > 999)
            {
                _loc_3 = 999;
            }
            if (_loc_5 > _loc_3)
            {
                _loc_5 = _loc_3;
            }
            if (_loc_3 == 0)
            {
                _loc_4 = 1;
            }
            if (this.data.hasOwnProperty("shoptype") && this.data.shoptype == 1)
            {
                this.Max = int(this.data.maxbuy);
            }
            else
            {
                this.Max = _loc_3;
            }
            this.amounttext.text = _loc_4 + "";
            var _loc_6:Boolean = true;
            this.visible = true;
            this.background.visible = _loc_6;
            return;
        }// end function

        private function darwItem() : void
        {
            if (this.item)
            {
                this.item.parent.removeChild(this.item);
            }
            var imgURL:* = new URLRequest();
            imgURL.url = Config.getUrl("assets/images/icon/tools/" + this.data.icon + ".png", ResourceType.NORMAL);
            this.item = new Loader();
            this.item.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function () : void
            {
                return;
            }// end function
            );
            this.item.load(imgURL);
            this.item.x = 59;
            this.item.y = 79;
            this.addChild(this.item);
            return;
        }// end function

        private function onClose(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        public function close() : void
        {
            this.curCount = 1;
            this.amounttext.text = this.curCount.toString();
            if (int(this.data.dz) > 0)
            {
                this.allpricetext.text = this.data.nowprice;
            }
            else
            {
                this.allpricetext.text = this.data.price;
            }
            var _loc_1:Boolean = false;
            this.visible = false;
            this.background.visible = _loc_1;
            this.data = null;
            return;
        }// end function

        private function onDown(event:MouseEvent) : void
        {
            event.stopImmediatePropagation();
            return;
        }// end function

        private function resizeHandler(event:Event) : void
        {
            this.background.width = GlobalLayer.instance.root.stage.stageWidth;
            this.background.height = GlobalLayer.instance.root.stage.stageHeight;
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            return;
        }// end function

    }
}
